package com.coke.spring.security.provider;

import cn.hutool.core.util.StrUtil;
import com.coke.spring.security.token.IpAuthenticationToken;
import com.coke.spring.security.token.MobelAuthenticationToken;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cwm
 * @Description 校验手机号码登录逻辑
 * @date 2021/8/26 下午1:32
 * @Version 1.0
 */
public class MobelAuthenticationProvider implements AuthenticationProvider {
    public static final Map<String, String> CAHE = new ConcurrentHashMap<String, String>(){
        {put("15707440460","1234");}
    };
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        MobelAuthenticationToken mobelAuthenticationToken =(MobelAuthenticationToken)authentication;
        String code = mobelAuthenticationToken.getCode();
        String phone = mobelAuthenticationToken.getPhone();
        if (StrUtil.isEmpty(CAHE.get(phone))){
            return null;
        }
        if (code.equals(CAHE.get(phone))){
            return new MobelAuthenticationToken(phone,code, Arrays.asList(new SimpleGrantedAuthority("admin")));
        }
        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
         return (MobelAuthenticationToken.class
                .isAssignableFrom(authentication));
    }
}
